package co.edu.usbcali.egrapico.vista.ambient;

import co.edu.usbcali.egrapico.vista.FrmPrincipal;
import co.edu.usbcali.egrapico.vista.tools.JPanelTranferHandlerProgram;

public class LabelProgram extends javax.swing.JLabel implements java.io.Serializable {

    PanelProgram frame;
    String nameParent;
    private javax.swing.JTextField TextField;
    private javax.swing.JTextPane TextPane;
    private javax.swing.JButton cancel;
    private javax.swing.JPanel comment;
    private javax.swing.JInternalFrame frame2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label;
    private javax.swing.JButton ok;
    private String toolTipText;
    private javax.swing.JMenuItem item1;
    private javax.swing.JMenuItem iten2;
    private javax.swing.JMenuItem iten4;
    private javax.swing.JSeparator separador;
    private javax.swing.JSeparator separador2;
    private javax.swing.JPopupMenu poput;
    boolean ban = false, ban1 = false;//si no ha sido creado

    /**
     * creates a new intance of LabelC
     */
    public LabelProgram() {
        toolTipText = "";
        frame = new PanelProgram();
        nameParent = "";
        poput = new javax.swing.JPopupMenu();
        item1 = new javax.swing.JMenuItem();
        iten2 = new javax.swing.JMenuItem();
        separador = new javax.swing.JSeparator();
        separador2 = new javax.swing.JSeparator();
        iten4 = new javax.swing.JMenuItem();
        item1.setText("Properties...");
        item1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item1ActionPerformed(evt);
            }
        });
        poput.add(item1);

        iten2.setText("Remove");
        iten2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item2ActionPerformed(evt);
            }
        });
        poput.add(iten2);
        poput.add(separador);
        poput.add(separador2);
        iten4.setText("View Definition Program");
        iten4.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item4ActionPerformed(evt);
            }
        });
        poput.add(iten4);
        setComponentPopupMenu(poput);
        //definicion de las propiedades  
        frame2 = new javax.swing.JInternalFrame("",
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        label = new javax.swing.JLabel();
        TextField = new javax.swing.JTextField();
        comment = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TextPane = new javax.swing.JTextPane();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        frame2.getContentPane().setLayout(new java.awt.FlowLayout());
        frame2.setFrameIcon(null);
        frame2.setMaximumSize(new java.awt.Dimension(79, 43));
        frame2.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        label.setText("Shape   ");
        frame2.getContentPane().add(label);
        TextField.setPreferredSize(new java.awt.Dimension(180, 19));
        frame2.getContentPane().add(TextField);
        comment.setLayout(null);
        comment.setBorder(javax.swing.BorderFactory.createTitledBorder("Comment"));
        comment.setPreferredSize(new java.awt.Dimension(250, 130));
        jScrollPane1.setViewportView(TextPane);
        comment.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 230, 100);
        frame2.getContentPane().add(comment);
        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        frame2.getContentPane().add(ok);
        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        frame2.getContentPane().add(cancel);
        frame2.setBounds(60, 50, 270, 230);
    }

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        TextField.setText(getName());
        TextPane.setText(getTextToolTip());
        frame2.setVisible(false);
    }

    private void okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        setName(TextField.getText());
        frame.setNameP(TextField.getText());
        setTextToolTip(getTextField() + ": " + TextPane.getText());
        frame2.setVisible(false);
        for (int i = 0; i < frame.getLPane().getComponentCount(); i++) {
            if (frame.getLPane().getComponent(i) instanceof LabelContext) {
                ((LabelContext) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelAsk) {
                ((LabelAsk) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelTell) {
                ((LabelTell) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelMsgSend) {
                ((LabelMsgSend) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            }

        }
    }

    private void item1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        if (PisCreate() == false) {
            PLadicionar();
        } else {
            PmostrarPanel();
        }
    }

    private void item2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        java.awt.Component suprim = getParent();
        frame2.setVisible(false);
        if (suprim instanceof LPaneContext) {
            ((PanelContext) ((LPaneContext) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resProgram();
            getFrame().setVisible(false);
            ((LPaneContext) suprim).remove(this);
            ((LPaneContext) suprim).repaint();
        } else if (suprim instanceof LPaneMethod) {
            ((PanelMethod) ((LPaneMethod) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resProgram();
            getFrame().setVisible(false);
            ((LPaneMethod) suprim).remove(this);
            ((LPaneMethod) suprim).repaint();
        } else if (suprim.getParent().getParent().getParent() instanceof PanelAsk) {
            getFrame().setVisible(false);
            ((PanelAsk) suprim.getParent().getParent().getParent()).setVisible(false);
            String namePP = ((PanelAsk) suprim.getParent().getParent().getParent()).getNameParentParent();
            String nameL = ((PanelAsk) suprim.getParent().getParent().getParent()).getNameParentLabel();
            for (int k = 0; k < ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponentCount(); k++) {
                if (((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k) instanceof LabelAsk) {
                    if (((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getText().equals(nameL)) {
                        if (((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).isClon()) {
                            ((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_ask1.gif")));
                        } else {
                            ((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/askPreceding.gif")));
                        }
                        ((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getFrame().setIsConsecuent(false);
                        ((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).changeItemMenu4();
                        ((LabelAsk) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).hideConsecuent();
                    }
                }
            }

        } else if (suprim.getParent().getParent().getParent() instanceof PanelTell) {
            getFrame().setVisible(false);
            ((PanelTell) suprim.getParent().getParent().getParent()).setVisible(false);
            String namePP = ((PanelTell) suprim.getParent().getParent().getParent()).getNameParentParent();
            String nameL = ((PanelTell) suprim.getParent().getParent().getParent()).getNameParentLabel();
            for (int k = 0; k < ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponentCount(); k++) {
                if (((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k) instanceof LabelTell) {
                    if (((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getText().equals(nameL)) {
                        if (((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).isClon()) {
                            ((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontell.gif")));
                        } else {
                            ((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tellPreceding.gif")));
                        }
                        ((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getFrame().setIsConsecuent(false);
                        ((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).changeItemMenu4();
                        ((LabelTell) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).hideConsecuent();
                    }
                }
            }

        } else if (suprim.getParent().getParent().getParent() instanceof PanelMsgSend) {
            getFrame().setVisible(false);
            ((PanelMsgSend) suprim.getParent().getParent().getParent()).setVisible(false);
            String namePP = ((PanelMsgSend) suprim.getParent().getParent().getParent()).getNameParentParent();
            String nameL = ((PanelMsgSend) suprim.getParent().getParent().getParent()).getNameParentLabel();
            for (int k = 0; k < ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponentCount(); k++) {
                if (((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k) instanceof LabelMsgSend) {
                    if (((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getText().equals(nameL)) {
                        if (((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).isClon()) {
                            ((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgSendPreceding.gif")));
                        } else {
                            ((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSendPreceding.gif")));
                        }
                        ((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).getFrame().setIsConsecuent(false);
                        ((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).changeItemMenu4();
                        ((LabelMsgSend) ((PanelProgram) FrmPrincipal.getInternalFrame(namePP)).getLPane().getComponent(k)).hideConsecuent();
                    }
                }
            }
        }
    }

    private void item4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        if (isCreate() == false) {
            Ladicionar();
        } else {
            mostrarPanel();
        }
    }
    //metodos para frame de propiedades ***************

    public PanelProgram getFrame() {
        return frame;
    }

    public void setTextToolTip(String t) {
        toolTipText = t;
    }

    public String getTextToolTip() {
        return toolTipText;
    }

    public void PmostrarPanel() {
        frame2.setVisible(true);
        try {
            frame2.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void PLadicionar() {
        TextField.setText(getName());
        FrmPrincipal.addElementtoDesKtop(frame2);
        frame2.setVisible(true);
        try {
            frame2.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        ban1 = true;
    }

    public boolean PisCreate() {
        return ban1;
    }

    public String getTextField() {
        return TextField.getText();
    }

    public String getTextPane() {
        return TextPane.getText();
    }

    public void setTextPane(String i) {
        TextPane.setText(i);
    }

    public void mostrarMenu(java.awt.Component c, int x, int y) {
        poput.show(c, x, y);
    }

    public void setNameParent(String name) {
        nameParent = name;
        for (int i = 0; i < frame.getLPane().getComponentCount(); i++) {
            if (frame.getLPane().getComponent(i) instanceof LabelContext) {
                ((LabelContext) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelAsk) {
                ((LabelAsk) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelTell) {
                ((LabelTell) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            } else if (frame.getLPane().getComponent(i) instanceof LabelMsgSend) {
                ((LabelMsgSend) frame.getLPane().getComponent(i)).setNameParent(nameParent + "." + getName());
            }
        }
    }

    public String getNameParent() {
        return nameParent;
    }

    public void setTitlePanel(String title) {
        frame.setTitle(title);
    }

    public String getTitlePanel() {
        return frame.getTitle();
    }

    public void mostrarPanel() {
        frame.setTitle(nameParent + "." + getName());
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void mostrar1Panel() {
        frame.setTitle(nameParent + ".Consecuent");
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void Ladicionar() {
        frame.setTransferHandler(new JPanelTranferHandlerProgram());
        frame.setTitle(nameParent + "." + getName());
        FrmPrincipal.addElementtoDesKtop(frame);
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        ban = true;
    }

    public void L1adicionar() {
        frame.setTitle(nameParent + ".Consecuent");
        FrmPrincipal.addElementtoDesKtop(frame);
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        ban = true;
    }

    public boolean isCreate() {
        return ban;
    }

    public boolean parsingC(String idO) {
        for (int i = 0; i < frame.getLPane().getComponentCount(); i++) {
            if (frame.getLPane().getComponent(i) instanceof LabelObject) {
                if (idO.equals(((LabelObject) frame.getLPane().getComponent(i)).getName())) {
                    return true;
                } else if (FrmPrincipal.getObjeto(((LabelObject) frame.getLPane().getComponent(i)).getName()).parsingC(idO)) {
                    return true;
                }
            } else if (frame.getLPane().getComponent(i) instanceof LabelAsk) {
                if (((LabelAsk) frame.getLPane().getComponent(i)).getFrame().getprogramL().parsingC(idO)) {
                    return true;
                }
            } else if (frame.getLPane().getComponent(i) instanceof LabelTell) {
                if (((LabelTell) frame.getLPane().getComponent(i)).getFrame().getprogramL().parsingC(idO)) {
                    return true;
                }
            } else if (frame.getLPane().getComponent(i) instanceof LabelMsgSend) {
                if (((LabelMsgSend) frame.getLPane().getComponent(i)).getFrame().getprogramL().parsingC(idO)) {
                    return true;
                }
            } else if (frame.getLPane().getComponent(i) instanceof LabelContext) {
                for (int j = 0; j < ((LabelContext) frame.getLPane().getComponent(i)).getFrame().getLPane().getComponentCount(); j++) {
                    if (((LabelContext) frame.getLPane().getComponent(i)).getFrame().getLPane().getComponent(j) instanceof LabelProgram) {
                        if (((LabelProgram) ((LabelContext) frame.getLPane().getComponent(i)).getFrame().getLPane().getComponent(j)).parsingC(idO)) {
                            return true;
                        }
                    }
                }
            }

        }
        return false;
    }
}